package academic.ui.client.view;


import java.util.ArrayList;
import java.util.List;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;


import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.MyDealsPresenter;
import academic.ui.client.widgets.DealsReportPage;
import ecommerce.soco.shared.GWT_Purchase;


public class MyDealsView extends ViewImpl implements MyDealsPresenter.MyView {

	GenericContainerTag dealsDiv = new GenericContainerTag("div");
	GenericContainerTag wizard = new GenericContainerTag("div");
	GenericContainerTag pages = new GenericContainerTag("div");
	
	GenericContainerTag back = new GenericContainerTag("a","Previous");
	GenericContainerTag next = new GenericContainerTag("a","Next");
	
	private GenericClickable showAllLink = new GenericClickable("a","show all");
	private GenericClickable giftsLink = new GenericClickable("a","gifts");

	@Inject
	public MyDealsView(CSSConstants css) {
		dealsDiv.setStyleName("dealsDiv");
		back.setStyleName("prev");
		next.setStyleName("next");
		wizard.getElement().setId("wizard");
		pages.setStyleName("items");
		dealsDiv.add(showAllLink);
		dealsDiv.add(giftsLink);
		showAllLink.setStyleName("selectLink");
		giftsLink.setStyleName("selectLink");
		showAllLink.addStyleName("selected");
		dealsDiv.add(wizard);
		wizard.add(pages);
		dealsDiv.add(back);
		dealsDiv.add(next);
	}
	
	@Override
	public Widget asWidget() {
		return dealsDiv;
	}

	@Override
	public void showMyDealsTab(ArrayList<GWT_Purchase> result) {
		pages.clear();
		//provide the user with a message if they have not yet purchased any deals
		if(result==null || result.size()<1)
		{
			GenericContainerTag noDealsPurchased = new GenericContainerTag("h3","Once you have purchased a deal you can view/print your deal from this tab.");
			pages.add(noDealsPurchased);
		}
		else
		{
			for(int i=0;i<result.size();i+=4) 
			{
				List<GWT_Purchase> deals = result.subList(i, Math.min(result.size(), i+4));
				
				GenericContainerTag page = new GenericContainerTag("div");
				pages.add(page);
				page.setStyleName("page");
					
				DealsReportPage report = new DealsReportPage();
				report.setup(deals);
				page.add(report);	
			}
			
			if(result.size()<4)
			{
				back.addStyleName("disabled");
				next.addStyleName("disabled");
			}
			else
			{
				next.removeStyleName("disabled");
			}
			
			wizard.setHeight(String.valueOf(Math.min(4,result.size())*125)+"px");
		}
		
	}
	
	@Override
	public HasClickHandlers getShowAll() {
		return showAllLink;
	}
	
	@Override
	public HasClickHandlers getShowGifts() {
		return giftsLink;
	}

	@Override
	public void setLinkSelected(String selectedLink) {
		if("gift".equals(selectedLink))
		{
			giftsLink.addStyleName("selectedLink");
			showAllLink.removeStyleName("selectedLink");
		}
		else
		{
			giftsLink.removeStyleName("selectedLink");
			showAllLink.addStyleName("selectedLink");
		}
	}
	
	
	
	
}
